<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */

declare(strict_types=1);

namespace App\Controller\V1;


use App\Annotation\AdminRequired;
use App\Annotation\GroupRequired;
use App\Annotation\Logger;
use App\Annotation\LoginRequired;
use App\Annotation\ParamValid;
use App\Annotation\PermissionMeta;
use App\Annotation\PermissionModule;
use App\Controller\AbstractController;
use App\Exception\NotFoundException;
use App\Request\Book\BookValidateRequest;
use App\Service\BookService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;

/**
 * @Controller(prefix="v1/book")
 * @PermissionModule(value="图书")
 * Class BookController
 * @package App\Controller\V1
 */
class BookController extends AbstractController
{
    /**
     * @Inject()
     * @var BookService
     */
    private $bookService;


    /**
     * @GetMapping("")
     * @GroupRequired
     * @PermissionMeta(value = "图书列表")
     * @return mixed
     */
    public function getBooks(){
        return $this->bookService->findAll();
    }

    /**
     * @GetMapping("buy")
     * @PermissionMeta(value="买图书")
     */
    public function buyBook(){
        var_dump(123123123);
    }

    /**
     * @GetMapping("search")
     * @LoginRequired()
     * @ParamValid(value="q",rule="alpha_dash")
     */
    public function searchBook(){
        $q = $this->request->query('q');
        return $this->bookService->getBookByKeyword($q);
    }

    /**
     * @GetMapping("{id}")
     * @PermissionMeta("图书详情")
     * @GroupRequired
     * @param $id
     * @return mixed
     */
    public function getBook($id){
        $book = $this->bookService->getById($id);
        if(!$book){
            throw new NotFoundException(10022);
        }
        return  $book;
    }

    /**
     * @PostMapping("")
     * @PermissionMeta(value="添加图书")
     * @GroupRequired
     * @Logger(template="id:{user.id}用户名：{user.nickname}添加了一本小黄书")
     * @param BookValidateRequest $request
     * @return mixed
     */
    public function addBook(BookValidateRequest $request){
        $params = $request->post();
        $this->bookService->createBook($params);
        return success(201,'新增小黄书成功',12);
    }

    /**
     * @PutMapping("{id}")
     * @GroupRequired
     * @PermissionMeta(value="编辑图书")
     * @param BookValidateRequest $request
     * @param $id
     * @return mixed
     */
    public function editBook(BookValidateRequest $request,$id){
        $params = $request->post();
        $params['id'] = $id;
        $this->bookService->updateBook($params);
        return success(201,'更新小黄书成功',13);
    }

    /**
     * @DeleteMapping("{id}")
     * @GroupRequired
     * @PermissionMeta(value="删除图书")
     * @param $id
     * @return mixed
     */
    public function deleteBook($id){
        $this->bookService->deleteById($id);
        return success(201,'删除图书成功',14);
    }


}